//
//  DYZForgetPsdController.m
//  DYZCars
//
//  Created by MF on 16/8/19.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZForgetPsdController.h"
#import "UILabel+Addition.h"
#import "UIButton+Addition.h"

@interface DYZForgetPsdController ()

@property (nonatomic, weak)UITextField *nameField;

@property (nonatomic, weak)UITextField *psdTextFidle;

@property (nonatomic, weak)UIButton *textBtn;
@end

@implementation DYZForgetPsdController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.title = @"忘记密码";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem  alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(backToLoginController)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem  alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(submitUser)];
    [self setupUI];
}

- (void)setupUI {
    //    图标
    UIImageView *iconView = [[UIImageView alloc] init];
    [self.view addSubview:iconView];
    
    //    帐号
    UILabel *nameLabel = [UILabel labelWithText:@"手机号" andTextColor:[UIColor blackColor] andFontSize:16];
    [nameLabel sizeToFit];
    [self.view addSubview:nameLabel];
    //    账户名textfield
    UITextField *nameField = [[UITextField alloc] init];
    nameField.placeholder = @"手机号";
    [self.view addSubview:nameField];
    //  线
    UIView *nameViewLine = [[UIView alloc] init];
    nameViewLine.backgroundColor = [UIColor blackColor];
    [self.view addSubview:nameViewLine];
    self.nameField = nameField;
    //    用户密码
    UILabel *psdLabel = [UILabel labelWithText:@"验证码" andTextColor:[UIColor blackColor] andFontSize:16];
    [psdLabel sizeToFit];
    [self.view addSubview:psdLabel];
    //    密码textfield
    UITextField *textTextFidle = [[UITextField alloc] init];
    textTextFidle.placeholder = @"验证码";
    [self.view addSubview:textTextFidle];
    self.psdTextFidle = textTextFidle;
    //  获取验证码
    UIButton *textBtn = [UIButton buttonWithText:@"获取" andTextColor:[UIColor blackColor] backgroundColor:[UIColor lightGrayColor] andFontSize:17];
    self.textBtn = textBtn;
    [textBtn addTarget:self action:@selector(loadTextMessage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:textBtn];
    //  线
    UIView *textViewLine = [[UIView alloc] init];
    textViewLine.backgroundColor = [UIColor blackColor];
    [self.view addSubview:textViewLine];
    
    //    用户新密码
    UILabel *newpsdLabel = [UILabel labelWithText:@"新密码" andTextColor:[UIColor blackColor] andFontSize:16];
    [newpsdLabel sizeToFit];
    [self.view addSubview:newpsdLabel];
    //    密码textfield
    UITextField *psdTextFidle = [[UITextField alloc] init];
    psdTextFidle.placeholder = @"新密码(6-12个字符)";
    psdTextFidle.secureTextEntry = YES;
    [self.view addSubview:psdTextFidle];
    self.psdTextFidle = psdTextFidle;
    //  线
    UIView *psdViewLine = [[UIView alloc] init];
    psdViewLine.backgroundColor = [UIColor blackColor];
    [self.view addSubview:psdViewLine];
//    自动布局
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.offset(200);
        make.height.offset(150);
        make.width.offset(150);
    }];
    
    //    帐号
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.offset(300);
    }];
    [nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel.mas_right).offset(8);
         make.width.offset(200);
        make.top.equalTo(nameLabel.mas_top);
        
    }];
    [nameViewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
        make.top.equalTo(nameField.mas_bottom).offset(4);
        make.height.offset(1);
    }];
    
    //    验证码
    
    [psdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom).offset(20);
    }];
    [textTextFidle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(psdLabel.mas_right).offset(8);
        make.top.equalTo(psdLabel.mas_top);
    }];
    [textViewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
        make.top.equalTo(textTextFidle.mas_bottom).offset(4);
        make.height.offset(1);
    }];
//    获取验证码的按钮
    [textBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.centerY.equalTo(textTextFidle);
        make.height.offset(30);
        make.width.offset(60);
    }];
    
    //    密码
    
    [newpsdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(psdLabel);
        make.top.equalTo(psdLabel.mas_bottom).offset(20);
    }];
    [psdTextFidle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(newpsdLabel.mas_right).offset(8);
        make.width.offset(200);
        make.top.equalTo(newpsdLabel.mas_top);
    }];
    [psdViewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
        make.top.equalTo(psdTextFidle.mas_bottom).offset(4);
        make.height.offset(1);
    }];
    
    
}
//返回登录界面
- (void)backToLoginController {
    [self dismissViewControllerAnimated:true completion:nil];
}
//提交用户信息
- (void)submitUser {
    NSLog(@"提交用户信息");
}
//获取验证码
- (void)loadTextMessage {
    [self performSelectorInBackground:@selector(time) withObject:nil];
}
- (void)time {
    self.textBtn.enabled = false;
    int second = 30;
    for (int i = 0; i < second; i++) {
        [NSThread sleepForTimeInterval:1];
        NSString *title = [NSString stringWithFormat:@"%zd秒",second - 1 - i];

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [self.textBtn setTitle:title forState:UIControlStateNormal];
        }];
        if (i == second - 1) {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                self.textBtn.enabled = true;
                [self.textBtn setTitle:@"获取" forState:UIControlStateNormal];
                return ;
            }];
        }
    }
}
@end
